import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/index.vue'

const routes = [
  {
    path: '/',
    // name: 'Index',
    component: Index,
    children: [
      {
        path: '',
        redirect:'/home'
      },
      {
        path:'/home',
        name:'home',
        component:()=>import("../views/Home.vue")
      },
      {
        path:'/order',
        name:'order',
        component:()=>import("../views/Order.vue")
      },
      {
        path:'/me',
        name:'me',
        component:()=>import("../views/Me.vue")
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('../views/Address.vue')
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


// 路由守卫
router.beforeEach((to, from, next) => {
  const isLOgin = localStorage.ele_login ? true : false;
  if (to.path == '/login') {
    next();
  } else {
    //是否在登录状态下
    isLOgin ? next() : next('/login');
  }
})

export default router
